package com.javapandeng.controller;

import com.javapandeng.base.BaseController;
import com.javapandeng.po.Comment;
import com.javapandeng.po.Message;
import com.javapandeng.service.CommentService;
import com.javapandeng.utils.Consts;
import com.javapandeng.utils.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 评论
 */
@Controller
@RequestMapping("/comment")
public class CommentController extends BaseController {

    @Autowired
    private CommentService commentService;

    /**
     * 添加执行
     */
    @RequestMapping("/exAdd")
    public String exAdd(Comment comment, HttpServletRequest request){
        Object attribute = request.getSession().getAttribute(Consts.USERID);
        if(attribute==null){
            return "redirect:/login/toLogin";
        }
        Integer userId = Integer.valueOf(attribute.toString());
        comment.setAddTime(new Date());
        comment.setUserId(userId);
        commentService.insert(comment);
        return "redirect:/itemOrder/my.action";
    }

    /**
     * 评价列表
     */
    @RequestMapping("/findBySql")
    public String findBySql(Comment comment, Model model){
        String sql = "select * from comment where 1=1 ";
        if(!isEmpty(comment.getContent())){
            sql += " and name content '%"+comment.getContent()+"%'";
        }
        sql += " order by id desc";
        Pager<Comment> pagers = commentService.findBySqlRerturnEntity(sql);
        model.addAttribute("pagers",pagers);
        model.addAttribute("obj",comment);
        return "comment/comment";
    }


    /**
     * 删除评价
     */
    @RequestMapping("/delete")
    public String delete(Integer id){
        commentService.deleteById(id);
        return "redirect:/comment/findBySql";
    }

    /**
     * 审核通过
     */
    @RequestMapping("/ok")
    public String ok(Integer id){
        Comment comment=this.commentService.getById(id);
        comment.setIsok(1);
        commentService.updateById(comment);
        return "redirect:/comment/findBySql";
    }

    /**
     * 审核拒绝
     */
    @RequestMapping("/error")
    public String error(Integer id){
        Comment comment=this.commentService.getById(id);
        comment.setIsok(2);
        commentService.updateById(comment);
        return "redirect:/comment/findBySql";
    }

}
